lobsterh5.main({
    data: {
        page: 1,//正在加载第1页
        perPage: 10,//每次加载10页
        flag: true,//是否完成最后一页加载
        loading: false,//本次请求是否完成
        patientlist: [],
        selectPatient: {},
    },
    pageload() {
        this.initevent();
        this.initdata();
    },
    initevent() {
        var self = this;

        $(document).on("click", "#patiengdiv", function () {
            $("#half").popup();
            self.inittemp("tplcard", "sidebaritem", { list: self.data.patientlist });
        })

        $(document).on("click", ".carditem", function (obj) {
            var card = obj.currentTarget.dataset.info;
            var info = JSON.parse(card);
            self.data.selectPatient = info;
            //测试就诊人 （身份证：320***********9739）
            $("#selectedPatient").html(info.Name + "  (" + info.IDCard + " )");
            $.closePopup()
            $("#div_data").html("");
            self.data.page = 1;
            self.data.flag = true;
            self.getrecord();
        });

        //下拉加载数据
        $('.container').infinite(100).on("infinite", function () {
            if (self.data.loading) return;
            self.data.loading = true;
            setTimeout(function () {
                self.data.page++;
                if (self.data.flag) {
                    self.getrecord();
                }
            }, 1000);
        });

        $(document).on("click", ".payitem", function () {
            var data = $(this).data("info");
            location.href = "paydetail.html?appid="+lobsterh5.GetAppId()+"&id=" + data.Id;
        });
    },
    initdata() {
        var self = this;
        lobsterh5.GET("/admin/v1/CPatient/GetUserPatient").then(res => {
            self.data.patientlist = res.patdata;
            self.getrecord();
        }).catch(res => {
            $.toast(res.msg, 'text');
        });
    },
    getrecord() {
        var self = this;
        $('#more').html('<i class="weui-loading"></i><span class="weui-loadmore__tips">正在加载</span>');
        $('#more').show();
        $.showLoading();
        var data = { "page": self.data.page, "limit": self.data.perPage };

        if (self.data.selectPatient) {
            data.PatientId = self.data.selectPatient.PatientId;
        }

        lobsterh5.GETAPI("tsselfpay.getpresrecordlist", data).then(res => {
            var list = res.list;

            if (list.length < self.data.perPage) {
                self.data.flag = false;//设置没有数据了标记
                $('#more').html("没有更多数据了");
                //$(document.body).destroyInfinite();
            } else {
                self.data.loading = false;
                $('#more').hide();
            }
            
            var html = template("tplrecord", { list: list });
            $("#div_data").append(html);
            $.hideLoading();
        }).catch(res=>{
            $.hideLoading();
        })
    },
    inittemp(tempid, _id, data) {
        var html = template(tempid, data);
        $("#" + _id).html(html);
    }
})